
import uuid
from flask import Blueprint, request
from conn_mysql import ConnectMysql


app_login = Blueprint('app_login', 'api')

@app_login.route('/login', methods=['POST'])
def login_api():

    user = request.form['user']
    pass_word = request.form['pass_word']

    if not all([user, pass_word]):#提交的账号和密码有空值

        return {
            "status": -1,
            "errorCode": 1,
            "errorMsg": "账号或密码不能为空",
            "resultBody": None,
        }

    with ConnectMysql() as c:

        sql = 'SELECT * FROM user_table WHERE user=%s and pass_word=%s'
        result = c.select_one(sql, (user, pass_word))

    if not result:

        return {
            "status": -1,
            "errorCode": 2,
            "errorMsg": "账号或密码有误",
            "resultBody": None,
        }
    else:

        access_token = uuid.uuid1().hex
        user_id = result['id']

        with ConnectMysql() as c:

            sql_ = "INSERT INTO access_token(user_id, access_token) VALUES (%s,%s)"
            c.insert_info(sql_, (user_id, access_token))
            print("插入token")

        return {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {"info":"登录成功","access_token": access_token, 'user_id': user_id},
        }






    


